package com.zlsy.springbootdemo.chainofresponsibility.mtdemo1;

/**
 * @author zhouliang
 * @date 2021/1/23 14:44
 */
public abstract class Handler {
  /** 指向下一个处理者 */
  private Handler nextHandler;
  /** 处理者能够处理的级别 */
  private int level;

  public Handler(int level) {
    this.level = level;
  }

  public void setNextHandler(Handler nextHandler) {
    this.nextHandler = nextHandler;
  }

  public final void handleMessage(Request request) {
    if (level == request.getLevel()) {
      this.echo(request);
    } else {
      if (this.nextHandler != null) {
        nextHandler.handleMessage(request);
      } else {
        System.out.println("已经到尽头了！！！");
      }
    }
  }

  // 抽象方法，子类实现
  public abstract void echo(Request request);
}
